/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> rst;
        queue<TreeNode*> queue;

        if(root != nullptr) queue.push(root);

        while(queue.empty() == false){
            int length = queue.size();

            for(int i = 0; i < length; ++i){
                TreeNode* temp = queue.front();

                if(temp->left != nullptr) queue.push(temp->left);
                if(temp->right != nullptr) queue.push(temp->right);

                if(i == length - 1){
                    rst.push_back(temp->val);
                }

                queue.pop();
            }     
        }

        return rst;
    }
};